<?php

class TicketsController extends AppController {

    public $sl_tickets = array(
        "actions" => array("V" => "view", "E" => "edit"),
        "filters" => array(
            array("name" => 'Ticket.id', "label" => 'Número', "presentation" => "TICKETS::NUMERO"),
            array("name" => 'Ticket.fecha_carga', "label" => 'Fecha Carga', "presentation" => "DATE"),
            array("name" => 'Ticket.prioridad', "label" => 'Prioridad', "presentation" => "TICKETS::PRIORIDAD"),
            array("name" => 'Ticket.origen', "label" => 'Origen', "presentation" => "TICKETS::ORIGEN"),
            array("name" => 'Ticket.estado', "label" => 'Estado', "presentation" => "TICKETS::ESTADO_TICKET"),
            array("name" => "Empresa.id", "label" => "Empresa", "presentation" => "SELECT", 'classparams' => '{"model":"Empresa"}'),
            array("name" => "Delegacion.id", "label" => "Delegación", "presentation" => "SELECT", 'classparams' => '{"model":"Delegacion"}')
        ),
        "columns" => array(
            array("name" => 'Ticket.id', "label" => 'Número', "presentation" => "TICKETS::NUMERO"),
            array("name" => 'Ticket.fecha_carga', "label" => 'Fecha Carga', "presentation" => "DATETIME"),
            array("name" => 'Ticket.prioridad', "label" => 'Prioridad', "presentation" => "TICKETS::PRIORIDAD"),
            array("name" => 'Ticket.origen', "label" => 'Origen', "presentation" => "TICKETS::ORIGEN"),
            array("name" => 'Ticket.estado', "label" => 'Estado', "presentation" => "TICKETS::ESTADO_TICKET"),
            array("name" => "Empresa.nombre", "label" => "Empresa"),
            array("name" => "Delegacion.nombre", "label" => "Delegación")
        )
    );
    public $maint_tickets = array(
        "fields" => array(
            "Datos del Cliente" => array(
                array("name" => "user_id", "label" => "Nombre", "presentation" => "AUTOCOMPLETE", 'classparams' => '{"model":"Admin.User"}'),
                array("name" => "User.email", "label" => "Email", 'readonly' => true),
                array("name" => "User.telefono", "label" => "Teléfono", 'readonly' => true),
                array("name" => "empresa_id", "label" => "Empresa", "presentation" => "SELECT", 'classparams' => '{"model":"Empresa"}', 'readonly' => true),
                array("name" => "delegacion_id", "label" => "Delegación", "presentation" => "SELECT", 'classparams' => '{"model":"Delegacion"}', 'readonly' => true)
            ),
            "Descripción del Ticket" => array(
                array("name" => "user_id_carga", "label" => "Usuario", "presentation" => "USER", 'classparams' => '{"force":true}', 'isvisible' => false, "actions" => "A"),
                array("name" => "fecha_carga", "label" => "Fecha Carga", "presentation" => "DATETIME", 'classparams' => '{"force":true}', 'isvisible' => false, "actions" => "A"),
                array("name" => "estado", "label" => "Estado", "presentation" => "TICKETS::ESTADO_TICKET", "initialvalue" => "1", "readonly" => true),
                array("name" => "tmp_tipo_inventario", "label" => "Tipo Solicitud", "presentation" => "TICKETS::TIPO_SOLICITUD", 'initialvalue' => "I", "actions" => "A|E"),
                array("name" => "inventario_id", "label" => "Código Inventario", "presentation" => "TICKETS::CODIGO_INVENTARIO", 'size' => '8'),
                array("name" => "tipo_id", "label" => "Dispositivo", 'presentation' => 'SELECT', 'classparams' => '{"model":"Tipo","order":"nombre ASC"}', "actions" => "A|E"),
                array("name" => "problema_id", "label" => "Problema", "presentation" => "PROBLEMAS"),
                array("name" => "descripcion", "label" => "Descripción", "presentation" => "TEXTAREA")
            ),
            "Servicio" => array(
                array("name" => "origen", "label" => "Origen", "presentation" => "TICKETS::ORIGEN", 'initialvalue' => "WEB"),
                array("name" => "prioridad", "label" => "Prioridad", "presentation" => "TICKETS::PRIORIDAD", 'initialvalue' => "3")
            )
        ),
        "tables" => array(
            "Eventos Ticket" => array(
                "model" => "TicketEvento",
                "fields" => array(
                    array("name" => "fecha_carga", "label" => "Fecha Carga", "presentation" => "DATETIME"),
                    array("name" => 'user_id', "label" => 'Usuario', 'presentation' => 'USER'),
                    array("name" => 'estado', "label" => 'Estado', "presentation" => "TICKETS::ESTADO_TICKET"),
                    array("name" => 'observacion', "label" => 'Observación', "presentation" => "TEXTAREA")
                ),
                "parentActions" => array("V", "E"),
                "actions" => array(),
                "order" => array('fecha_carga ASC')
            )
        ),
        "jsincludes" => array("tickets/maint_tickets.js")
    );
    public $editar_ticket = array(
        "fields" => array(
            "Datos del Cliente" => array(
                array("name" => "Ticket.user_id", "label" => "Nombre", "presentation" => "USER", 'readonly' => true),
                array("name" => "User.email", "label" => "Email", 'readonly' => true),
                array("name" => "User.telefono", "label" => "Teléfono", 'readonly' => true),
                array("name" => "Ticket.empresa_id", "label" => "Empresa", "presentation" => "SELECT", 'classparams' => '{"model":"Empresa"}', 'readonly' => true),
                array("name" => "Ticket.delegacion_id", "label" => "Delegación", "presentation" => "SELECT", 'classparams' => '{"model":"Delegacion"}', 'readonly' => true)
            ),
            "Descripción del Ticket" => array(
                array("name" => "estado", "label" => "Estado", "presentation" => "TICKETS::ESTADO_TICKET"),
                array("name" => "Ticket.inventario_id", "label" => "Código Inventario", "presentation" => "TICKETS::CODIGO_INVENTARIO", "readonly" => true),
                array("name" => "Ticket.problema_id", "label" => "Problema", "presentation" => "PROBLEMAS", "readonly" => true),
                array("name" => "Ticket.descripcion", "label" => "Descripción", "presentation" => "TEXTAREA", "readonly" => true)
            ),
            "Servicio" => array(
                array("name" => "Ticket.origen", "label" => "Origen", "presentation" => "TICKETS::ORIGEN", "readonly" => true),
                array("name" => "Ticket.prioridad", "label" => "Prioridad", "presentation" => "TICKETS::PRIORIDAD", "readonly" => true),
                array("name" => "tmp_observacion", "label" => "Observación", "presentation" => "TEXTAREA")
            )
        ),
        "tables" => array(
            "Eventos Ticket" => array(
                "model" => "TicketEvento",
                "fields" => array(
                    array("name" => "TicketEvento.fecha_carga", "label" => "Fecha Carga", "presentation" => "DATETIME"),
                    array("name" => 'TicketEvento.user_id', "label" => 'Usuario', 'presentation' => 'USER'),
                    array("name" => 'TicketEvento.estado', "label" => 'Estado', "presentation" => "TICKETS::ESTADO_TICKET"),
                    array("name" => 'TicketEvento.observacion', "label" => 'Observación', "presentation" => "TEXTAREA")
                ),
                "parentActions" => array("V", "E"),
                "actions" => array(),
                "order" => array('fecha_carga ASC')
            )
        ),
        "jsincludes" => array("tickets/maint_tickets.js")
    );

    public function add() {
        // Envio los mails
        if ($this->request->is('post') || $this->request->is('put')) {
            $email = new CakeEmail();
            $email->transport('DB');
            $email->from(array('me@example.com' => 'My Site'));
            $email->to(array($this->Auth->user('email') => $this->Auth->user('name')));
            $email->subject('Nuevo Ticket');
            $email->send('Se ha generado un nuevo ticket.');

            if ($this->Auth->user('jefe') == "No") {
                $model = $this->getModelName();
                $jefe = $this->$model->Query("SELECT name, email FROM users WHERE jefe='Si' AND empresa_id=" . $this->Auth->user('Empresa.id') . " AND delegacion_id=" . $this->Auth->user('Delegacion.id'));

                if (!empty($jefe)) {
                    $email = new CakeEmail();
                    $email->transport('DB');
                    $email->from(array('me@example.com' => 'My Site'));
                    $email->to(array($jefe['users']['email'] => $jefe['users']['name']));
                    $email->subject('Nuevo Ticket');
                    $email->send('Se ha generado un nuevo ticket.');
                }
            }
        }

        $this->maint = $this->maint_tickets;
        parent::add();
    }

    public function edit($id = null) {
        $this->maint = $this->editar_ticket;
        parent::edit($id);
    }

    public function index($last = null) {
        $this->search_list = $this->sl_tickets;
        parent::index($last);
    }

    public function view($id = null) {
        $this->maint = $this->maint_tickets;
        parent::view($id);
    }

}